package beerAir;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;

public class FlightController {
	
	@SuppressWarnings("unchecked")
	public List <Flight> getTimesFromAirDate(HomeView v){
		
		EntityManagerFactory factory;
		EntityManager manager;
		
		factory= Persistence.createEntityManagerFactory("BeerAir");
		manager = factory.createEntityManager();
		
		Airport depAirport = null;
		Airport arrAirport = null;
		List<Flight> a=new ArrayList<Flight>();
		
		try{
		Query querydep=manager.createQuery("SELECT a FROM Airport a WHERE a.icaoCode=:arg1", Airport.class);
		querydep.setParameter("arg1", v.getDepAirport());
		
		depAirport=(Airport) querydep.getSingleResult();
		
		Query queryarr=manager.createQuery("SELECT a FROM Airport a WHERE a.icaoCode=:arg2", Airport.class);
		queryarr.setParameter("arg2", v.getArrAirport());
		
		arrAirport=(Airport) queryarr.getSingleResult();
		
		Query query=manager.createQuery("SELECT f FROM Flight f WHERE f.departureDate=:arg3 "
				+ "AND f.departureAirport=:arg4 AND f.arrivalAirport=:arg5", Flight.class);
				
		query.setParameter("arg3", v.getDepDate());
		query.setParameter("arg4", depAirport);
		query.setParameter("arg5", arrAirport);
		
		a=query.getResultList();
		}catch(NoResultException e) {
			return null;
		}
		return a;
		
	}

}
